package com.frank.practice.controller;

import com.frank.common.entity.Result;
import com.frank.practice.api.req.*;
import com.frank.practice.api.vo.RankVo;
import com.frank.practice.api.vo.ReportVo;
import com.frank.practice.api.vo.ScoreDetailVo;
import com.frank.practice.api.vo.SubjectDetailVo;
import com.frank.practice.service.PracticeDetailService;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/6/19
 * @description
 */
@RestController
@RequestMapping("/practice/detail")
public class PracticeDetailController {

    private final PracticeDetailService practiceDetailService;

    @Autowired
    public PracticeDetailController(PracticeDetailService practiceDetailService) {
        this.practiceDetailService = practiceDetailService;
    }

    /**
     * 提交题目
     *
     * @param req 提交题目请求
     * @return 提交结果
     */
    @PostMapping("/submitSubject")
    public Result<Boolean> submitSubject(@RequestBody SubmitSubjectDetailReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getPracticeId()), "练习id不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectId()), "题目id不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectType()), "题目类型不能为空！");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getTimeUse()), "用时不能为空！");
        return Result.ok(practiceDetailService.submitSubject(req));
    }

    /**
     * 提交练习
     *
     * @param req 提交练习请求
     * @return 提交结果
     */
    @PostMapping("/submit")
    public Result<Boolean> submitPractice(@RequestBody SubmitPracticeDetailReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getSetId()), "套题id不能为空！");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getSubmitTime()), "交卷时间不能为空！");
        Preconditions.checkArgument(StringUtils.isNotBlank(req.getTimeUse()), "用时不能为空！");
        return Result.ok(practiceDetailService.submitPractice(req));
    }

    /**
     * 获取答案解析-每题得分
     *
     * @param req params
     * @return 得分详情列表
     */
    @GetMapping("/getScoreDetail")
    public Result<List<ScoreDetailVo>> getScoreDetail(GetScoreDetailReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getPracticeId()), "练习id不能为空！");
        return Result.ok(practiceDetailService.getScoreDetail(req));
    }

    /**
     * 获取答案解析-答题详情
     *
     * @param req params
     * @return 答题详情
     */
    @GetMapping("/getSubjectDetail")
    public Result<SubjectDetailVo> getSubjectDetail(GetSubjectDetailReq req) {
        Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectId()), "题目id不能为空！");
        Preconditions.checkArgument(Objects.nonNull(req.getSubjectType()), "题目类型不能为空！");
        return Result.ok(practiceDetailService.getSubjectDetail(req));
    }

    /**
     * 获取报告信息
     *
     * @param practiceId 练习 ID
     * @return 报告信息
     */
    @GetMapping("/getReport")
    public Result<ReportVo> getReport(Long practiceId) {
        Preconditions.checkArgument(Objects.nonNull(practiceId), "练习id不能为空！");
        return Result.ok(practiceDetailService.getReport(practiceId));
    }

    /**
     * 获取练习排行榜
     * @return 练习排行榜
     */
    @GetMapping("/getPracticeRankList")
    public Result<List<RankVo>> getPracticeRankList() {
        return Result.ok(practiceDetailService.getPracticeRankList());
    }

    /**
     * 放弃练习
     * @param req 放弃练习请求参数
     * @return 放弃练习结果
     */
    @PostMapping("/giveUp")
    public Result<Boolean> giveUp(@RequestBody GiveUpPracticeReq req) {
        Preconditions.checkArgument(Objects.nonNull(req.getPracticeId()), "练习id不能为空！");
        return Result.ok(practiceDetailService.giveUp(req));
    }
}
